package com.hongsen.cn.Utils;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.hongsen.cn.dayandmonth.R;

public class GlideUtil {

    @SuppressLint("CheckResult")
    public static RequestBuilder<Drawable> newCacheInstance(Context context, String url){
        return Glide.with(context).load(url)
                .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                .apply(RequestOptions.placeholderOf(R.drawable.icon_no_img));
    }

    public static void newCache(Context context, String url, int imageW, int imageH){
        Glide.with(context).load(url).submit(imageW, imageH);
    }

    public static void newCacheInstance(Context context, final String url, final ImageView imageView){
        newCacheInstance(context,url, R.drawable.icon_no_img,imageView);
    }

    public static void newCacheInstance(Context context, String url, int placeholderRes, ImageView imageView){
        Glide.with(context).load(url)
                .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                .apply(RequestOptions.placeholderOf(placeholderRes))
                .into(imageView);
    }

    @SuppressLint("CheckResult")
    public static void newCacheInstance(Context context, String url, RequestListener requestListener){
        newCacheInstance(context,url).addListener(requestListener).submit();
    }

    /**
     * 加载本地资源
     * @param id
     * @param imageView
     */
    public static void newCacheInstance(Context context, int id, ImageView imageView){
        Glide.with(context).load(id)
                .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.AUTOMATIC))
                .into(imageView);
    }
}

